function card_csvAnsysTempCurve(curve,iPrefix,iMID)
 {
      // curve is a nested array [series][X = 0, Y = 1]
	  //int ikeyString = 1 for EX (e.g.)
	  //int iPrefix 0 = MPTEMP,1 = FC
      curve = sortCurve(curve);
	  var s = "";
      var X = 0;
      var Y = 1;  
	  var sc = ",";
	  var ss = "";
	  if(iPrefix == 0)
	  	 ss = "MPTEMP,1";
	  else if(iPrefix == 1)
		 ss = "FC" + sc + iMID + sc + "TEMP" + sc;
		s += ss;
		for(var i=0;i<curve.length;i++)
            s += sc + formatRealToExponential(curve[i][X]);
		s += "\n";

      return s;   
}

// This function prints the datapoints in multiple lines
function card_csvAnsysIsoDynamicCurve(curve, iPrefix, numPointsPerLine, label, iMID){
	// curve is a nested array [series][X = 0, Y = 1]
	//int ikeyString = 1 for EX (e.g.)
	//int iPrefix 0 = MPTEMP,1 = FC
	var s = "";
	var X = 0;
	var Y = 1;  
	var sc = ",";
	var ss = "";
	if(iPrefix == 0){
		ss = "MPTEMP";
		s = "\nMPTEMP";
	} else if(iPrefix == 1){
		ss = "MPDATA" + sc + ss + label + sc + ss + iMID;
	}
	curve = sortCurve(curve);
	for(var i=0;i<curve.length;i++){
		if(i%numPointsPerLine == 0){
			if(iPrefix==0){
				s += "\n";
				s += ss;
				s += sc + "\t" + (i+1) + sc + "\t" + formatRealToExponential(curve[i][X]);
			}else{
				s += "\n";
				s += ss;
				s += sc + "\t" + (i+1) + sc + "\t" + formatRealToExponential(curve[i][Y]);
			}
		}else{
			if(iPrefix==0){
				s += sc+ "\t" + formatRealToExponential(curve[i][X]);
			}else{
				s += sc+ "\t" + formatRealToExponential(curve[i][Y]);
			}
		}
	}
	//s += "\n";
	return s;   
}

function card_csvAnsysDynamicCurveX(curve, numPointsPerLine){
	var s = "";
	var X = 0;
	var Y = 1;  
	var sc = ",";
	var ss = "MPTEMP";
	var iTotalCurves = curve.length;
	s += ss + "\n";
	for(var i=0;i<iTotalCurves;i++){
		if(i%numPointsPerLine == 0){
			s += ss + sc + "\t" + (i+1) + sc + "\t" + formatRealToExponential(curve[i][X]);
		}else{
			s += sc+ "\t" + formatRealToExponential(curve[i][X]);
			if((i+1)%numPointsPerLine == 0){
				s+= "\n";
			}
		}
	}
	s+= "\n";
	return s;
}

function card_csvAnsysDynamicCurveY(curve, numPointsPerLine, label, iMID){
	var s = "";
	var X = 0;
	var Y = 1;  
	var sc = ",";
	var iTotalCurves = curve.length;
	var ss = "MPDATA" + sc + " " + label + sc + " " + iMID;

	for(var i=0;i<iTotalCurves;i++){
		if(i%numPointsPerLine == 0){
			s += ss + sc + "\t" + (i+1) + sc + "\t" + formatRealToExponential(curve[i][Y]);
		}else{
			s += sc+ "\t" + formatRealToExponential(curve[i][Y]);
			if((i+1)%numPointsPerLine == 0){
				s+= "\n";
			}
		}	
	}
	s+="\n";
	return s;
}	

// This is Wrapper function which is used for Ansys solver in order to display the data in the format as specified by Siemens
function card_csvAnsysCurveXY(curve, numPointsPerLine, label, iMID){
	curve = sortCurve(curve);
	var s = "";
	s += card_csvAnsysDynamicCurveX(curve, numPointsPerLine);
	s += card_csvAnsysDynamicCurveY(curve, numPointsPerLine, label, iMID);
	return s;
}

function card_csvAnsysTempDataCurve(curve,ikeyString,iKeyMID)
 {
      // curve is a nested array [series][X = 0, Y = 1]
	  //int ikeyString = EX (e.g.)
      var s = "";
      var X = 0;
      var Y = 1;  
	  var ss = ikeyString;
	  var sc = ",";
	  
	  var ss_mpdata = "MPDATA,";
		s += ss_mpdata + ss + sc + iKeyMID;
		for(var i=0;i<curve.length;i++)
		{		
            s += sc +formatRealToExponential(curve[i][Y]);
		}      
		s += "\n";
      
      return s;   
}

function card_csvAnsysTempFCCurve(curve,sFC,iMID) {
      // curve is a nested array [series][X = 0, Y = 1]
	  //iFCIndex 0 = STvsTempCAE 1 = SCvsTempCAE 2 = SSvsTempCAE
	  //FC, ${MID}, S, XTEN,
      var s = "";
	  var sc = ",";	  
	  var ss = "FC,";
	  ss+= iMID;	  
	  ss+= sc + "S" + sc;	  
	  ss += sFC;s += ss;
	  var iNum = 1; //for Failure data  	
      for(var i=0;i<curve.length;i++)
            s += sc + formatRealToExponential(curve[i][iNum]);
      s += "\n";
      return s;   
}

function card_ansysCreepModel(curves, iMID, creepOption, numPointsPerLine) {
	var X = 0;
	var Y = 1;
	var s = "";
	var xVariable = "Temps";
	curves = getValidCurvesFromAllCurves(curves);
	if(curves.length == 0)
		return s;
	
	var validCurve = getValidCurve(curves);
	var sc = ",";
	var iTotalCurves = curves.length;	
	var numPoints = validCurve.length;
	s+= String.format("TB,CREE, %s, %d, %d, %d", iMID, numPoints, curves.length, creepOption);
	var ss = "TBDATA" + sc;
	for(var iPoint = 0; iPoint < numPoints; iPoint++) {
		s += "\n";
		s+= String.format("TBFIELD,%s,%s", xVariable, formatReal(validCurve[iPoint][X]));
		s += "\n";
		for(var iCurve=0;iCurve<iTotalCurves;iCurve++){
			var curve = curves[iCurve];
			if(iCurve%numPointsPerLine == 0){
				s += ss + (iCurve+1) + sc + formatRealToExponential(curve[iPoint][Y]);
			}else{
				s += sc+ formatRealToExponential(curve[iPoint][Y]);
				if((iCurve+1)%numPointsPerLine == 0 && (iCurve + 1) < iTotalCurves){
					s+= "\n";
				}
			}	
		}
	}
	s += "\n";
	return s;
}

function card_ansysPlasticFamilyOfCurvesAtTemperature(curveFamily, iMID, tempParamName, curveInputName) {
	var X = 0;
	var Y = 1;
	var s = "";
	var xVariable = "Temps";
	
	/*
	 * TB,PLAS,_MATL   ,   7,,MISO
	   TBFIELD,Temps,20.000000
       TBPT,DEFI,0.000000e+00,8.100000e+02
	 */
	var familyParameterGroups = curveFamily.getFamilyParameterGroups();
	var numParameterGroups = familyParameterGroups.size();
	s += String.format("TB,PLAS, %s, %d,,MISO\n",iMID, numParameterGroups);
	
	for(var i = 0; i < numParameterGroups; i++) {
		var paramSet = familyParameterGroups.get(i);
		var parameter =  paramSet.getParameterByName(tempParamName);
		var paramValue = 0;
		if(parameter != null) {
			paramValue = parameter.getValue();
			if(paramValue == null)
				paramValue = 0;
		}
		s += String.format("TBFIELD,%s,%s\n", xVariable, formatRealToExponential(paramValue));
		
		var curveInput = paramSet.getInputByName(curveInputName);
		var curve = curveInput.getValue();
		for(var j = 0; j < curve.length; j++) {
			s += String.format("TBPT,DEFI,%s,%s\n", formatRealToExponential(curve[j][X]), formatRealToExponential(curve[j][Y]));
		}
	}
	return s;
}

function card_ansysStrengthRatiosAtTemperature(curveFamily, iMID, tempParamName, numPointsPerLine) {
	var X = 0;
	var Y = 1;
	var s = "";
	var xVariable = "Temps";
	
	/*
	 * ! HIll Coefficients
	TB,HILL,_MATL   ,   8
	TBTEM,  20.0000000    ,   1
	TBDAT,   1, 1.000000000    , 0.800000000    , 0.800000000    
	TBDAT,   4, 1.000000000    , 0.800000000    , 1.000000000    
	TBTEM,  400.000000    ,   2
	TBDAT,   1, 1.000000000    , 0.850000000    , 0.850000000    
	TBDAT,   4, 1.000000000    , 0.850000000    , 1.000000000 
	 */
	var familyParameterGroups = curveFamily.getFamilyParameterGroups();
	var numParameterGroups = familyParameterGroups.size();
	s += String.format("TB,HILL, %s, %d\n",iMID, numParameterGroups);
	
	var pointField = "TBDATA,  ";
	var comma = ",";
	for(var i = 0; i < numParameterGroups; i++) {
		var paramSet = familyParameterGroups.get(i);
		var parameter =  paramSet.getParameterByName(tempParamName);
		var paramValue = 0;
		if(parameter != null) {
			paramValue = parameter.getValue();
			if(paramValue == null)
				paramValue = 0;
		}
		s += String.format("TBTEMP, %s\n", formatRealToExponential(paramValue));
		
		var inputs = paramSet.getFamilyInputs();
		var numInputs = inputs.size();
		for(var j = 0; j < numInputs; j++) {
			var hillCoefficientInput = inputs.get(j);
			var coeffValue = hillCoefficientInput.getValue();
			if(j%numPointsPerLine == 0){
				s += pointField + (j+1) + comma + formatRealToExponential(coeffValue);
			}else{
				s += comma + formatRealToExponential(coeffValue);
				if((j+1)%numPointsPerLine == 0 && (j + 1) < numInputs){
					s+= "\n";
				}
			}
		}
		s+= "\n";
	}
	return s;
}
